﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageControlsGetByPageIdDataCommand : CmsPageControlsGetByPageIdCommand
    {
        public override IQueryable<CmsPageControl> Execute()
        {
            IEnumerable<CmsPageControl> result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPageControl>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPageControl>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPageControl, int>>();

                result = SqlUtility<CmsPageControl, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageControlsSelectByPageId",
                        new
                        {
                            PageId = CommandParameter.PageId,
                            IsInAdministration = CommandParameter.IsInAdministrationArea,
                            PageControlTypeParameter = "PageControl" //TO DO: remove hardcoded value
                        },
                        mapper);
            }

            return result != null ?
                result.AsQueryable() :
                new List<CmsPageControl>().AsQueryable();
        }
    }
}
